<?php

class ColorController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/column2';

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 * Creates a new model.
	 * In ajax mode, the grid will be refreshed when the creation is successful
	 * , while in normal mode, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new Color;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);
        
        if(isset($_POST['Color']))
		{
			$model->attributes=$_POST['Color'];
			if($model->save())
            {
                if(Yii::app()->request->isAjaxRequest)
                {
                	// Stop jQuery from re-initialization
			        Yii::app()->clientScript->scriptMap['jquery.js'] = false;

                    echo CJSON::encode(array(
                        'status' => 'success',
                        'content' => '<div class="tips tips-ok clearfix">
						        <i class="ico-ok"></i>
						        <div class="cont">
						            <h4>Congratulations!</h4>
						            <p>Color: '.$model->name.' successfully added.</p>
						        </div>
						    </div>',
						'model'=>$model,
                    ));
                    exit;
                }
                else
                    $this->redirect(array('view','id'=>$model->id));
            }
		}

		if(Yii::app()->request->isAjaxRequest)
        {
        	// Stop jQuery from re-initialization
	        Yii::app()->clientScript->scriptMap['jquery.js'] = false;

            echo CJSON::encode(array(
                'status' => 'failure',
                'content' => $this->renderPartial('_form', array('model'=>$model), true, true),
            ));
        }
        else
            $this->render('create',array('model'=>$model));
	}

	/**
	 * Updates a particular model.
	 * In ajax mode, the grid will be refreshed when the update is successful
	 * , while in normal mode, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Color']))
		{
			$model->attributes=$_POST['Color'];
			if($model->save())
            {
                if(Yii::app()->request->isAjaxRequest)
                {
                	// Stop jQuery from re-initialization
			        Yii::app()->clientScript->scriptMap['jquery.js'] = false;

                    echo CJSON::encode(array(
                        'status' => 'success',
                        'content' => '<div class="tips tips-ok clearfix">
						        <i class="ico-ok"></i>
						        <div class="cont">
						            <h4>Congratulations!</h4>
						            <p>Color: '.$model->name.' successfully updated.</p>
						        </div>
						    </div>',
                    ));
                    exit;
                }
                else
                    $this->redirect(array('view','id'=>$model->id));
            }
		}

		if(Yii::app()->request->isAjaxRequest)
        {
        	// Stop jQuery from re-initialization
	        Yii::app()->clientScript->scriptMap['jquery.js'] = false;

            echo CJSON::encode(array(
                'status' => 'failure',
                'content' => $this->renderPartial('_form', array('model'=>$model), true, true),
            ));
        }
        else
            $this->render('update',array('model'=>$model,));
	}

	/**
	 * Deletes a particular model.
	 * In ajax mode, the grid will be refreshed when the update is successful
	 * , while in normal mode, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		$model = $this->loadModel($id);

		if(Yii::app()->request->isAjaxRequest)
		{
		    // Stop jQuery from re-initialization
			Yii::app()->clientScript->scriptMap['jquery.js'] = false;

			if(isset($_POST['action']) && $_POST['action'] == 'confirmDelete')
			{
				$model->delete();
				echo CJSON::encode( array(
					'status' => 'success',
					'content' => '<div class="tips tips-ok clearfix">
				        <i class="ico-ok"></i>
				        <div class="cont">
				            <h4>Info</h4>
				            <p>Deleted succussfully.</p>
				        </div>
				    </div>',
					));
				exit;
			}
			else if(isset($_POST['action']))
			{
				echo CJSON::encode( array(
					'status' => 'canceled',
					'content' => '<div class="tips tips-info clearfix">
				        <i class="ico-info"></i>
				        <div class="cont">
				            <h4>Info</h4>
				            <p>Deleted canceled.</p>
				        </div>
				    </div>',
					));
				exit;
			}
			else
			{
				echo CJSON::encode( array(
					'status' => 'failure',
					'content' => $this->renderPartial('delete', array(
						'model' => $model ), true, true),
					));
				exit;
			}
		}
		else
		{
			if(isset($_POST['confirmDelete']))
			{
				$model->delete();
				$this->redirect(array('admin'));
			}
			else if(isset($_POST['denyDelete']))
				$this->redirect(array('view','id' => $model->id));
			else
				$this->render('delete', array('model' => $model));
		}
	}

	/**
	 * Manages all models.
	 */
	public function actionIndex()
	{
		$model=new Color('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Color']))
			$model->attributes=$_GET['Color'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Color::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='color-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
